package Fly.example.service.impl;

import Fly.example.common.CustomException;
import Fly.example.pojo.Category;
import Fly.example.mapper.CategoryMapper;
import Fly.example.pojo.Dish;
import Fly.example.pojo.Setmeal;
import Fly.example.service.ICategoryService;
import Fly.example.service.IDishService;
import Fly.example.service.ISetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author 陽Fly
 * @since 2025-06-13
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private IDishService dishService;

    @Override
    public void remove(String ids) {
        //查询有没有菜品关联该套餐或者是菜品
        LambdaQueryWrapper<Dish> dishwrapper = new LambdaQueryWrapper<Dish>();
        dishwrapper.eq(Dish::getCategoryId, ids);
        int count = dishService.count(dishwrapper);
        if (count > 0) {
            //大于0表示已经关联有菜品，抛出异常，异常的类型应该是我们自己定义的
            throw new CustomException("该菜品关联有菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealwrapper = new LambdaQueryWrapper<>();
        setmealwrapper.eq(Setmeal::getCategoryId, ids);
        int count1 = setmealService.count(setmealwrapper);
        if (count1 > 0) {
            //大于0表示已经关联有套餐，抛出异常，异常的类型应该是我们自己定义的
            throw new CustomException("该菜品关联有套餐，不能删除");
        }

        //否则执行删除
        super.removeById(ids);

    }
}
